using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RichControlLibrary;
using System.Text.RegularExpressions;

namespace RichControlLibraryDemo
{
    public partial class ValidationControlForm : Form
    {
        public ValidationControlForm()
        {
            InitializeComponent();
            this.Font = SystemFonts.DialogFont;

            DigitsValidator vDigits = Validator.Digits;
            txtDigitsOnly.Validator = vDigits;
            chkDigitsHex.Checked = vDigits.AllowsHexDigits;

            NumberValidator vNum = Validator.Number;
            txtNumber.Validator = vNum;
            chkNumDecimalFraction.Checked = vNum.AllowsDecimalFraction;
            chkNumThousands.Checked = vNum.AllowsThousandsSeparator;
            cmbNumDecSymbol.Text = vNum.DecimalSeparatorSymbol;
            cmbNumThousandsSymbol.Text = vNum.ThousandsSeparatorSymbol;
            chkNumAutoOmitPosSign.Checked = vNum.AutoOmitPositiveSign;
            chkNumAutoGroup.Checked = vNum.AutoGroupByThousands;

            EnglishLettersValidator vEngLetters = Validator.EnglishLetters;
            txtEnglishLettersOnly.Validator = vEngLetters;
            switch (vEngLetters.Casing)
            {
                case ValidationCasingPolicy.UpperAndLowerCases:
                    cmbEngLettersCasing.SelectedIndex = 0;
                    break;
                case ValidationCasingPolicy.UppercaseOnly:
                    cmbEngLettersCasing.SelectedIndex = 1;
                    break;
                case ValidationCasingPolicy.LowercaseOnly:
                    cmbEngLettersCasing.SelectedIndex = 2;
                    break;
            }

            txtEmailAddress.Validator = Validator.EmailAddress;
            txtPhoneNumber.Validator = Validator.PhoneNumber;
            txtLetterNumberAndDash.Validator = new SimpleRegexValidator(@"^[0-9a-zA-Z\-]+$");
        }

        private void txt_InvalidInput(object sender, EventArgs e)
        {
            Control txt = sender as Control;
            txt.BackColor = Color.LightPink;
        }

        private void txt_ValidInput(object sender, EventArgs e)
        {
            Control txt = sender as Control;
            txt.BackColor = SystemColors.Window;
        }

        private void chkDigitsHex_CheckedChanged(object sender, EventArgs e)
        {
            (txtDigitsOnly.Validator as DigitsValidator).AllowsHexDigits = chkDigitsHex.Checked;
            txtDigitsOnly.Validate();
        }

        private void chkNumDecimalFraction_CheckedChanged(object sender, EventArgs e)
        {
            (txtNumber.Validator as NumberValidator).AllowsDecimalFraction = chkNumDecimalFraction.Checked;
            txtNumber.Validate();
        }

        private void chkNumThousands_CheckedChanged(object sender, EventArgs e)
        {
            (txtNumber.Validator as NumberValidator).AllowsThousandsSeparator = chkNumThousands.Checked;
            txtNumber.Validate();
        }

        private void cmbNumDecSymbol_Validated(object sender, EventArgs e)
        {
            (txtNumber.Validator as NumberValidator).DecimalSeparatorSymbol = cmbNumDecSymbol.Text;
            txtNumber.Validate();
        }

        private void cmbNumThousandsSymbol_Validated(object sender, EventArgs e)
        {
            (txtNumber.Validator as NumberValidator).ThousandsSeparatorSymbol = cmbNumThousandsSymbol.Text;
            txtNumber.Validate();
        }

        private void chkNumAutoOmitPosSign_CheckedChanged(object sender, EventArgs e)
        {
            (txtNumber.Validator as NumberValidator).AutoOmitPositiveSign = chkNumAutoOmitPosSign.Checked;
            txtNumber.Validate();
        }

        private void chkNumAutoGroup_CheckedChanged(object sender, EventArgs e)
        {
            (txtNumber.Validator as NumberValidator).AutoGroupByThousands = chkNumAutoGroup.Checked;
            txtNumber.Validate();
        }

        private void cmbEngLettersCasing_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnglishLettersValidator validator = txtEnglishLettersOnly.Validator as EnglishLettersValidator;
            switch (cmbEngLettersCasing.SelectedIndex)
            {
                case 0:
                    validator.Casing = ValidationCasingPolicy.UpperAndLowerCases;
                    break;
                case 1:
                    validator.Casing = ValidationCasingPolicy.UppercaseOnly;
                    break;
                case 2:
                    validator.Casing = ValidationCasingPolicy.LowercaseOnly;
                    break;
            }
            txtEnglishLettersOnly.Validate();
        }
    }
}